package com.glory.good_dark.activity;

import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.UnlockEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author yi.zhang
 * @date 2019/6/18
 * @desc 解锁界面
 */
public class UnlockActivity extends BaseProjectActivity {
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.EQPID_Txt)
    TextView EQPIDTxt;
    @BindView(R.id.tvLockType)
    TextView tvLockType;
    @BindView(R.id.ivLockTypeListArrow)
    ImageView ivLockTypeListArrow;
    @BindView(R.id.flLockTypeList)
    FrameLayout flLockTypeList;

    private String Unlock = "";

    private List<String> lockTypeList;
    private FreeUI_GeneralPop lockTypePop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_unlockactivity;
    }

    @Override
    protected void initView() {
        EQPIDTxt.setText(App.EQPID);
        lockTypeList = new ArrayList<>();
    }

    @Override
    protected void initData() {
//        String ss = "{\n" +
//                "    \"data\":{\n" +
//                "        \"HEADER\":{\n" +
//                "            \"MACHINENAME\":\"PDA-MT9001\",\n" +
//                "            \"MESSAGENAME\":\"PDAUnlockInfo\",\n" +
//                "            \"TRANSACTIONID\":\"c5e5e318-389d-4c39-b914-e4c10ba519d4\",\n" +
//                "            \"EVENTTIME\":\"2019-03-16 16:28:59.585\"\n" +
//                "        },\n" +
//                "        \"BODY\":{\n" +
//                "            \"EQP_ID\":\"LC128\",\n" +
//                "            \"UNLOCK_TYPE_LIST\":[\n" +
//                "                {\n" +
//                "                    \"EQP_TYPE\":\"T0001\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_TYPE\":\"手动锁机1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\",\n" +
//                "                    \"LOCK_STATES\":\"1\",\n" +
//                "                    \"LOCK_USER\":\"admin\",\n" +
//                "                    \"LOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"UNLOCK_USER\":\"admin\",\n" +
//                "                    \"UNLOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"EQP_LOCK_ID\":122\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"EQP_TYPE\":\"T0001\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_TYPE\":\"设备参数报警/999999999999999\",\n" +
//                "                    \"LOCK_STATES\":\"1\",\n" +
//                "                    \"LOCK_USER\":\"admin\",\n" +
//                "                    \"LOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"UNLOCK_USER\":\"admin\",\n" +
//                "                    \"UNLOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"EQP_LOCK_ID\":121\n" +
//                "                },\n" +
//                "                {\n" +
//                "                    \"EQP_TYPE\":\"T0001\",\n" +
//                "                    \"PROCESS\":\"TS00001\",\n" +
//                "                    \"LOCK_TYPE\":\"暂停锁机\",\n" +
//                "                    \"LOCK_STATES\":\"1\",\n" +
//                "                    \"LOCK_USER\":\"admin\",\n" +
//                "                    \"LOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"UNLOCK_USER\":\"admin\",\n" +
//                "                    \"UNLOCK_TYPE_TIME\":\"2029-03-16 16:28:59\",\n" +
//                "                    \"EQP_LOCK_ID\":120\n" +
//                "                }\n" +
//                "            ]\n" +
//                "        },\n" +
//                "        \"Return\":{\n" +
//                "            \"RETURNCODE\":\"0\",\n" +
//                "            \"RETURNMESSAGE\":\"\"\n" +
//                "        }\n" +
//                "    }\n" +
//                "}\n" +
//                "\n";
//
//                        BaseEntity<UnlockEntity> bean = (new Gson()).fromJson(ss, new TypeToken<BaseEntity<UnlockEntity>>() {
//        }.getType());
//
//        List<UnlockEntity.UnLockTypeEntity> unLockTypeList = bean.getData().getBody().getUNLOCK_TYPE_LIST();
//        lockTypeList.clear();
//        if (unLockTypeList.size() > 0){
//            for (UnlockEntity.UnLockTypeEntity entity : unLockTypeList){
//                lockTypeList.add(entity.getLOCK_TYPE());
//            }
//        }else {
//            showToast("当前设备无需解锁",false);
//            UnlockActivity.this.finish();
//        }

        getInfo();
    }

    private void getInfo() {
        //获取解锁类型
        FreeUI_EntityCallBack<BaseEntity<UnlockEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<UnlockEntity>>
                (new TypeToken<BaseEntity<UnlockEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<UnlockEntity>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    List<UnlockEntity.UnLockTypeEntity> unLockTypeList = response.body().getData().getBody().getUNLOCK_TYPE_LIST();
                    lockTypeList.clear();
                    if (unLockTypeList.size() > 0){
                        for (UnlockEntity.UnLockTypeEntity entity : unLockTypeList){
                            lockTypeList.add(entity.getLOCK_TYPE() + "/" + entity.getEQP_LOCK_ID());
                        }
                    }else {
                        showToast("当前设备无需解锁",false);
                        UnlockActivity.this.finish();
                    }
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<UnlockEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAUnlockInfo");
            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAUnlockInfo", jsonObject);
    }

    private void submit() {
//        showToast("解锁成功",true);
//        initData();
//        tvLockType.setText("");
//        Unlock = "";
        FreeUI_EntityCallBack<BaseEntity<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<JSONObject>>
                (new TypeToken<BaseEntity<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<JSONObject>> response) {
                if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                    showToast("解锁成功",true);
                    tvLockType.setText("");
                    Unlock = "";
                    getInfo();
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAUnlock");
            }
        };

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQP_ID", App.EQPID);
        jsonObject.addProperty("EQP_LOCK_ID", Unlock);
        NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAUnlock", jsonObject);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("解锁");
        freeUI_titleView.setRightListener("提交", v -> {
            Unlock = tvLockType.getText().toString().trim();
            if (Unlock.equals("")) {
                showToast("未获取到解锁ID，请选择解锁类型", false);
            } else {
                FreeApi_DialogUtils.getConfirmAndCancelDialog(App.app, "您确定要解锁吗？", () -> submit(), () -> {
                }).show(getSupportFragmentManager(), "UnLock_DIALOG");
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @OnClick(R.id.flLockTypeList)
    public void onViewClicked() {
        if (lockTypeList.size() <= 0) {
            showToast("未获取到解锁类型", false);
            return;
        }

        if (lockTypePop != null) {
            if (lockTypePop.isShowing()) {
                lockTypePop.dismiss();
            }
        } else {
            lockTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flLockTypeList.getWidth(), lockTypeList, ivLockTypeListArrow, (code, i) -> {
                tvLockType.setText(code);
                Unlock = code.split("/")[1];
            });
        }
        FreeApi_PopUtils.setArrowShow(ivLockTypeListArrow);
        lockTypePop.showAsDropDown(flLockTypeList, 0, 0);

    }

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {

    }
}
